<?php
/* post type for our clients */
$labels = array(
    'name'                  => _x('Our Clients', 'post type general name', 'dt'),
    'singular_name'         => _x('Our Clients', 'post type singular name', 'dt'),
    'add_new'               => _x('Add New', 'post type new', 'dt'),
    'add_new_item'          => __('Add New Client', 'dt'),
    'edit_item'             => __('Edit', 'dt'),
    'new_item'              => __('New Client', 'dt'),
    'view_item'             => __('View', 'dt'),
    'search_items'          => __('Search', 'dt'),
    'not_found'             =>  __('No Items Found', 'dt'),
    'not_found_in_trash'    => __('No Items Found in Trash', 'dt'), 
    'parent_item_colon'     => '',
    'menu_name'             => 'Our Clients'
);

$img = get_template_directory_uri(). '/images/admin_ico_clients.png';

$args = array(
    'labels'                => $labels,
    'public'                => false,
    'publicly_queryable'    => false,
    'show_ui'               => true, 
    'show_in_menu'          => true, 
    'query_var'             => true,
    'rewrite'               => false,
    'capability_type'       => 'post',
    'has_archive'           => false, 
    'hierarchical'          => false,
    'menu_position'         => 30,
    'menu_icon'		        => $img,
    'supports'              => array( 'title', 'thumbnail' )
); 
register_post_type( 'dt_our_clients', $args );
			
// metaboxez
add_action( 'add_meta_boxes', 'dt_clients_box' );

/* Do something with the data entered */
add_action( 'save_post', 'dt_clients_save' );

function dt_clients_box() {
    add_meta_box(
        'clients_box',
        __( 'Options', LANGUAGE_ZONE ),
        'dt_clients_box_func',
        'dt_our_clients'
    );
}

function dt_clients_box_func( $post ) {
    $boxname = 'our_clients';
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), $boxname. '_nonce' );
    
    $data = get_post_meta( $post->ID, $boxname, true );
    $defaults = array(
        'link'    => ''
    );
    $options = wp_parse_args( $data, $defaults );
    ?>
    <p>
        <label for="<?php echo $boxname; ?>_link"><?php _e('Link', LANGUAGE_ZONE); ?></label><br/>
        <input name="<?php echo $boxname; ?>_link" value="<?php echo esc_attr($options['link']); ?>" id="<?php echo $boxname; ?>_link" style="width: 85%" />
    </p>
    <?php
}
			
function dt_clients_save( $post_id ) {
    $boxname = 'our_clients';
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
  
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times

    if ( !isset( $_POST[$boxname. '_nonce'] ) || !wp_verify_nonce( $_POST[$boxname. '_nonce'], plugin_basename( __FILE__ ) ) )
        return;

    // Check permissions
    if ( !current_user_can( 'edit_page', $post_id ) )
        return;
    
    $mydata = null;
    $mydata['link'] = $_POST[$boxname. '_link'];
    
    update_post_meta( $post_id, $boxname, $mydata );
}